//
//  CheckOffAndTitleView.m
//  YIJU2.0-HD
//
//  Created by 陈柏兵 on 16/5/11.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "CheckOffAndTitleView.h"
#import "Macro.h"
#import "Masonry.h"
#import "ValidView.h"

@interface CheckOffAndTitleView()
@property (nonatomic, strong) ValidView *checkOff;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *touchSelect;
@end

@implementation CheckOffAndTitleView
- (instancetype)initWithCheckIsSelected:(CheckOffIsSelected)isSelected {
    if (self = [super init]) {
        self.isSelectedBlock = isSelected;
    }
    return self;
}


- (void)selectTheCheckOff:(UIButton *)sender {
    if (sender.selected == NO) {
        sender.selected = YES;
        self.isSelectedBlock(sender.selected);
        _checkOff.type = ValidViewCorrect;
    } else {
        sender.selected = NO;
        self.isSelectedBlock(sender.selected);
        _checkOff.type = ValidViewEmpty;
    }
}

- (void)setUpContainer {
    [self.checkOff mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.mas_left);
        make.width.equalTo(self.mas_height).multipliedBy(2/3.0f);
        make.height.equalTo(self.mas_height).multipliedBy(2/3.0f);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_checkOff.mas_top);
        make.left.equalTo(_checkOff.mas_right).offset(TenOffsetViewMargin);
        make.width.lessThanOrEqualTo(self.mas_width).priorityLow(30);
        make.bottom.equalTo(_checkOff.mas_bottom);
    }];
    
    [self.touchSelect mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.checkOff.mas_top);
        make.left.equalTo(self.checkOff.mas_left);
        make.bottom.equalTo(self.checkOff.mas_bottom);
        make.right.equalTo(self.checkOff.mas_right);
    }];
}

- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = _titleString;
        _titleLabel.font = kTitle_Font;
        _titleLabel.textColor = [UIColor blackColor];
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (ValidView *)checkOff {
    if (_checkOff == nil) {
        _checkOff = [[ValidView alloc] init];
        _checkOff.backgroundColor = [UIColor clearColor];
        _checkOff.layer.borderWidth = 1.0f;
        _checkOff.layer.borderColor = [UIColor redColor].CGColor;
    _checkOff.userInteractionEnabled = YES;
        [self addSubview:_checkOff];
    }
    return _checkOff;
}

- (UIButton *)touchSelect {
    if (_touchSelect == nil) {
        _touchSelect = [UIButton buttonWithType:UIButtonTypeCustom];
        [_touchSelect addTarget:self action:@selector(selectTheCheckOff:) forControlEvents:UIControlEventTouchUpInside];
        [_checkOff addSubview:_touchSelect];
    }
    return _touchSelect;
}

- (void)setTitleString:(NSString *)titleString {
    _titleString = titleString;
    [self setUpContainer];
}


@end
